package com.github.bitsapling.sapling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.bitsapling.sapling.config.BaseInfoProperties;
import com.github.bitsapling.sapling.mapper.PromotionPolicyMapper;
import com.github.bitsapling.sapling.mapper.TorrentMapper;
import com.github.bitsapling.sapling.mapper.UserMapper;
import com.github.bitsapling.sapling.entity.PromotionPolicy;
import com.github.bitsapling.sapling.entity.Torrent;
import com.github.bitsapling.sapling.controller.bo.TorrentUploadBO;
import com.github.bitsapling.sapling.service.CategoryService;
import com.github.bitsapling.sapling.service.TorrentService;
import com.github.bitsapling.sapling.utils.PagedGridResult;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;



@Service
public class TorrentServiceImpl extends BaseInfoProperties implements TorrentService {
    @Resource
    private TorrentMapper torrentMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private PromotionPolicyMapper promotionPolicyMapper;

    @Override
    public Torrent createOrUpdate(@NotNull Torrent torrent) {
        if (torrent.getId() != null) {
            return torrentMapper.updateById(torrent) == 1 ? torrent : null;
        }
        return torrentMapper.insert(torrent) > 0 ? torrent : null;
    }

    @Override
    public Torrent getTorrentByHash(@NotNull String infoHash) {
        infoHash = infoHash.toLowerCase();
        return torrentMapper.selectOne(
                new QueryWrapper<Torrent>().eq("info_hash", infoHash)
        );
    }

    @Override
    public List<Torrent> queryAllReviewed() {
        return torrentMapper.selectList(
                new QueryWrapper<Torrent>().eq("under_review", 1)
        );
    }

    @Override
    public List<Torrent> queryAllUnreviewed() {
        return torrentMapper.selectList(
                new QueryWrapper<Torrent>().eq("under_review", 0)
        );
    }

    @Override
    public Torrent getTorrentById(long id) {
        return torrentMapper.selectOne(
                new QueryWrapper<Torrent>().eq("id", id)
        );
    }

    @Override
    public boolean reviewTorrent(String infoHash, int isApproved) {
        Torrent torrent = torrentMapper.selectOne(new QueryWrapper<Torrent>().eq("info_hash", infoHash));
        if (torrent == null) {
            return false;
        }
        torrent.setUnderReview(isApproved == 1);
        return torrentMapper.updateById(torrent) > 0;
    }

    @Override
    public PagedGridResult getTorrentsByCategoryId(Long categoryId, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        // 使用 QueryWrapper 查询 category_id 等于传入参数的种子记录
        List<Torrent> list =  torrentMapper.selectList(
                new QueryWrapper<Torrent>().eq("category_id", categoryId)
        );
        return setterPagedGrid(list, page);
    }

    @Override
    public List<Torrent> searchTorrents(String keyword) {
        String searchKeyword = "%" + keyword + "%";
        return torrentMapper.selectList(
                new QueryWrapper<Torrent>()
                        .like("tags", searchKeyword)
                        .or()
                        .like("description", searchKeyword)
        );
    }

    @Override
    public long applyTorrentPromotionUploadOffset(Torrent torrent, long promotionUploadOffset) {
        PromotionPolicy promotionPolicy = promotionPolicyMapper.selectById(torrent.getPromotionPolicyId());
        if (promotionPolicy == null) {
            return promotionUploadOffset;
        }
        return (long) promotionPolicy.applyUploadRatio(promotionUploadOffset);
    }

    @Override
    public long applyTorrentPromotionDownloadOffset(Torrent torrent, long promotionDownloadOffset) {
        PromotionPolicy promotionPolicy = promotionPolicyMapper.selectById(torrent.getPromotionPolicyId());
        if (promotionPolicy == null) {
            return promotionDownloadOffset;
        }
        return (long) promotionPolicy.applyDownloadRatio(promotionDownloadOffset);
    }

    @Override
    public boolean deleteTorrent(Torrent torrent) {
        return torrentMapper.deleteById(torrent.getId()) > 0;
    }

}
